package com.neusoft.yiyang.customer.controller;

import com.neusoft.yiyang.common.entity.customer.OutingRequest;
import com.neusoft.yiyang.customer.service.OutingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Tag(name = "外出申请管理接口", description = "负责客户外出申请的创建、审批、回院登记等全流程管理")
@RestController
@RequestMapping("/outing-requests")
public class OutingController {

    @Autowired
    private OutingService outingService;

    @Operation(summary = "创建外出申请", description = "客户提交外出申请，包含外出时间、原因、陪同人等信息")
    @PostMapping("/createOutingRequest")
    public Map<String, Object> createOutingRequest(
            @Parameter(description = "外出申请信息（含客户ID、外出时间、原因等）", required = true)
            @RequestBody OutingRequest outingRequest) {
        return outingService.createOutingRequest(outingRequest);
    }

    @Operation(summary = "查询所有外出申请", description = "获取系统中所有外出申请及关联的客户、审批信息")
    @GetMapping
    public List<Map<String, Object>> getAllOutingRequests() {
        return outingService.getAllOutingRequests();
    }

    @Operation(summary = "查询护工负责的外出申请", description = "获取指定护工所负责客户的外出申请列表")
    @GetMapping("/getByCaretakerId/{caretakerId}")
    public List<Map<String, Object>> getOutingRequestsByCaretakerId(
            @Parameter(description = "护工ID", required = true) @PathVariable Integer caretakerId) {
        return outingService.getOutingRequestsByCaretakerId(caretakerId);
    }

    @Operation(summary = "根据ID查询外出申请", description = "获取指定外出申请的详细信息")
    @GetMapping("/{outingRequestId}")
    public OutingRequest getOutingRequestById(
            @Parameter(description = "外出申请ID", required = true) @PathVariable Integer outingRequestId) {
        return outingService.getOutingRequestById(outingRequestId);
    }

    @Operation(summary = "更新外出申请状态", description = "修改外出申请的审批状态（如待审批、已同意、已拒绝、已外出等）")
    @PutMapping("/{outingRequestId}/status")
    public void updateOutingRequestStatus(
            @Parameter(description = "外出申请ID", required = true) @PathVariable Integer outingRequestId,
            @Parameter(description = "状态（如pending/agreed/rejected/out/returned）", required = true)
            @RequestParam String status) {
        outingService.updateOutingRequestStatus(outingRequestId, status);
    }

    @Operation(summary = "同意外出申请", description = "将外出申请状态设为“已同意”")
    @GetMapping("/agree/{outingRequestId}")
    public void agreeOutingRequest(
            @Parameter(description = "外出申请ID", required = true) @PathVariable Integer outingRequestId) {
        outingService.agreeOutingRequest(outingRequestId);
    }

    @Operation(summary = "确认客户外出", description = "记录客户实际外出时间，将状态设为“已外出”")
    @GetMapping("/confirmOut/{outingRequestId}")
    public void confirmOut(
            @Parameter(description = "外出申请ID", required = true) @PathVariable Integer outingRequestId) {
        outingService.confirmOut(outingRequestId);
    }

    @Operation(summary = "拒绝外出申请", description = "将外出申请状态设为“已拒绝”")
    @GetMapping("/reject/{outingRequestId}")
    public void rejectOutingRequest(
            @Parameter(description = "外出申请ID", required = true) @PathVariable Integer outingRequestId) {
        outingService.rejectOutingRequest(outingRequestId);
    }

    @Operation(summary = "按客户姓名搜索外出申请", description = "根据客户姓名模糊查询相关的外出申请")
    @GetMapping("/search/{customerName}")
    public List<Map<String, Object>> searchByCustomerName(
            @Parameter(description = "客户姓名（支持模糊匹配）", required = true) @PathVariable String customerName) {
        return outingService.searchByCustomerName(customerName);
    }

    @Operation(summary = "回院登记", description = "记录客户实际回院时间，将状态设为“已回院”")
    @PutMapping("/return/{outingRequestId}")
    public void returnOutingRequest(
            @Parameter(description = "外出申请ID", required = true) @PathVariable Integer outingRequestId,
            @Parameter(description = "实际回院时间（格式：yyyy-MM-dd HH:mm:ss）", required = true)
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date actualReturnTime
    ) {
        outingService.returnOutingRequest(outingRequestId, actualReturnTime);
    }

    @Operation(summary = "删除外出申请", description = "移除指定的外出申请记录")
    @DeleteMapping("/{outingRequestId}")
    public void deleteOutingRequest(
            @Parameter(description = "外出申请ID", required = true) @PathVariable Integer outingRequestId) {
        outingService.deleteOutingRequest(outingRequestId);
    }

    @Operation(summary = "编辑外出申请", description = "修改外出申请的信息（如外出时间、原因、陪同人等）")
    @PutMapping("/update/{outingRequestId}")
    public void updateOutingRequest(
            @Parameter(description = "外出申请ID", required = true) @PathVariable Integer outingRequestId,
            @Parameter(description = "更新后的外出申请信息", required = true) @RequestBody OutingRequest updateRequest) {
        outingService.updateOutingRequest(outingRequestId, updateRequest);
    }
}